package cn.com.jslh.boarpro.base;

import android.app.Application;

import com.blankj.utilcode.util.Utils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.MemoryCookieStore;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import cn.com.jslh.boarpro.tables.BreedRecordTable;
import cn.com.jslh.boarpro.tables.DuanNaiTable;
import cn.com.jslh.boarpro.tables.ModelTable;
import cn.com.jslh.boarpro.tables.RecordHeaderTable;
import cn.com.jslh.boarpro.tables.StriplineTable;
import cn.com.jslh.boarpro.tables.WeanTable;
import cn.com.jslh.boarpro.tables.ZhouHaoTable;
import cn.com.jslh.boarpro.tables.ZhuSheTable;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.database.DBHelper;
import cn.com.jslh.boarpro.util.template.AddTempBaseItem;
import cn.com.jslh.boarpro.util.template.BaseAction;
import okhttp3.OkHttpClient;

/**
 * 作者：周洲 on 2017/7/12 11:29
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class BaseApplication extends Application {

    public static String Userid = "USERID";
//    public static String ServerUrl = "http://192.168.99.55:56898/ormrpc/services/";
//         public static String ServerUrl ="http://192.168.2.124:56898/ormrpc/services/";
//    public static String ServerUrl = "http://192.168.2.124:56898/ormrpc/services/";

    public static String ServerUrl = "http://58.216.233.172:6890/ormrpc/services/";
    public static String upAppUrl="http://58.216.233.171:81/lh-api";
    public static String FORGNAME = "FORGNAME";
    public static String FUSERID = "FUSERID";
    public static String FLINEID = "FLINEID";
    public static String FLINENAME = "FLINENAME";
    public static String FUSERNAME = "FUSERNAME";
    public static String FORGID = "FORGID";
    public static String EASSESSIONID = "EASSESSIONID";
    public static String MODULETYPE = "MODULETYPE";
    public static String FCOUNT=""; //角标数量
    public static List headerList = new ArrayList();

    @Override
    public void onCreate() {
        super.onCreate();
        setConfig();
        initOkGo();
    }

    public void setConfig() {
        Utils.init(this);
        WebService.init(ServerUrl, ServerUrl);//初始化 WebService
        DBHelper.ClassList.add(StriplineTable.class);
        DBHelper.ClassList.add(ModelTable.class);
        DBHelper.ClassList.add(ZhouHaoTable.class);
        DBHelper.ClassList.add(BreedRecordTable.class);
        DBHelper.ClassList.add(WeanTable.class);
        DBHelper.ClassList.add(ZhuSheTable.class);
        DBHelper.ClassList.add(DuanNaiTable.class);
        DBHelper.ClassList.add(BaseAction.class);
        DBHelper.ClassList.add(AddTempBaseItem.class);
        DBHelper.ClassList.add(RecordHeaderTable.class);
    }

    private  void initOkGo( ) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
//log打印级别，决定了log显示的详细程度
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
//log颜色级别，决定了log在控制台显示的颜色
        loggingInterceptor.setColorLevel(Level.INFO);
        builder.addInterceptor(loggingInterceptor);

        //全局的读取超时时间
        builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
//全局的写入超时时间
        builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
//全局的连接超时时间
        builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
//使用sp保持cookie，如果cookie不过期，则一直有效
//        builder.cookieJar(new CookieJarImpl(new SPCookieStore(this)));
////使用数据库保持cookie，如果cookie不过期，则一直有效
//        builder.cookieJar(new CookieJarImpl(new DBCookieStore(this)));
//使用内存保持cookie，app退出后，cookie消失
        builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));

        OkGo.getInstance().init(this)                       //必须调用初始化
                .setOkHttpClient(builder.build())               //必须设置OkHttpClient
                .setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST)               //全局统一缓存模式，默认不使用缓存，可以不传
                .setCacheTime(300)   //全局统一缓存时间秒，默认永不过期，可以不传 5分钟
                .setRetryCount(0);   //全局统一超时重连次数，默认为三次，那么最差的情况会请求4次(一次原始请求，三次重连请求)，不需要可以设置为0
//                .addCommonHeaders(headers)                      //全局公共头
//                .addCommonParams(params);                       //全局公共参数

    }
}
